home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 October / enter-2005-10.iso / files / jedit42install.exe / {app} / macros / Misc / Show_Threads.bsh < prev   
Encoding:
Text File  |  2004-08-29  |  3.9 KB  |  151 lines

  1. /* Show_Threads.bsh
  2.  *
  3.  * A BeanShell macro script for jEdit - displays all threads in a tree.
  4.  *
  5.  * Copyright (c) 2001 Dirk Moebius (dmoebius@gmx.net)
  6.  *
  7.  * This program is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU General Public License
  9.  * as published by the Free Software Foundation; either version 2
  10.  * of the License, or any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  *
  21.  *
  22.  * Version 1.0
  23.  * requires JDK 1.1, jEdit3.1
  24.  *
  25.  * $Id: Show_Threads.bsh,v 1.2 2002/01/31 05:35:48 spestov Exp $
  26.  */
  27.  
  28.  
  29. import java.awt.event.*;
  30. import javax.swing.*;
  31. import javax.swing.tree.*;
  32.  
  33.  
  34. JFrame createThreadsFrame() {
  35.  
  36.     void windowClosing(WindowEvent e) {
  37.         GUIUtilities.saveGeometry(frame, "macros-show-threads");
  38.     }
  39.  
  40.  
  41.     void keyPressed(KeyEvent e) {
  42.         if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
  43.             GUIUtilities.saveGeometry(frame, "macros-show-threads");
  44.             frame.setVisible(false);
  45.             frame.dispose();
  46.         }
  47.         else if (e.getKeyCode() == KeyEvent.VK_U) {
  48.             tree.setModel(new DefaultTreeModel(createModel()));
  49.             tree.revalidate();
  50.         }
  51.     }
  52.  
  53.  
  54.     // Having these members of KeyListener and WindowListener implemented as
  55.     // no-ops will speedup execution.  Otherwise BeanShell throws an
  56.     // exception each time one of these events occur.
  57.     void keyReleased(KeyEvent e) { }
  58.     void keyTyped(KeyEvent e) { }
  59.     void windowActivated(WindowEvent e) { }
  60.     void windowClosed(WindowEvent e) { }
  61.     void windowDeactivated(WindowEvent e) { }
  62.     void windowDeiconified(WindowEvent e) { }
  63.     void windowIconified(WindowEvent e) { }
  64.     void windowOpened(WindowEvent e) { }
  65.  
  66.  
  67.     ThreadGroup getMainThreadGroup() {
  68.         t = Thread.currentThread();
  69.         g = t.getThreadGroup();
  70.         while (g.getParent() != null)
  71.             g = g.getParent();
  72.         return g;
  73.     }
  74.  
  75.  
  76.     DefaultMutableTreeNode createModel() {
  77.         g = getMainThreadGroup();
  78.         return createGroupNode(g);
  79.     }
  80.  
  81.  
  82.     DefaultMutableTreeNode createGroupNode(ThreadGroup g) {
  83.         node = new DefaultMutableTreeNode(g.getName() + (g.isDaemon() ? " (daemon)" : ""), true);
  84.  
  85.         groups = new ThreadGroup[g.activeGroupCount()];
  86.         count = g.enumerate(groups, false);
  87.         for (int i = 0; i < count; ++i)
  88.             node.add(createGroupNode(groups[i]));
  89.  
  90.         threads = new Thread[g.activeCount()];
  91.         count = g.enumerate(threads, false);
  92.         for (int i = 0; i < count; ++i)
  93.             node.add(new DefaultMutableTreeNode(getDescription(threads[i]), false));
  94.  
  95.         return node;
  96.     }
  97.  
  98.  
  99.     String getDescription(Thread t) {
  100.         StringBuffer buf = new StringBuffer(t.getName());
  101.         buf.append(" (prio ");
  102.         buf.append(t.getPriority());
  103.         if (!t.isAlive())
  104.             buf.append(", not started");
  105.         if (t.isDaemon())
  106.             buf.append(", daemon");
  107.         if (t.isInterrupted())
  108.             buf.append(", interrupted");
  109.         buf.append(")");
  110.         return buf.toString();
  111.     }
  112.  
  113.  
  114.     tree = new JTree(createModel());
  115.     tree.putClientProperty("JTree.lineStyle", "Angled");
  116.     tree.addKeyListener(this);
  117.  
  118.     stage = new JScrollPane(tree);
  119.     stage.setColumnHeaderView(new JLabel("[Esc] Close      [U] Update"));
  120.  
  121.     frame = new JFrame("Current Threads");
  122.     frame.setContentPane(stage);
  123.     frame.addWindowListener(this);
  124.     // set default size and position:
  125.     frame.setSize(new Dimension(400, 400)); // faster than pack()
  126.     // overwrite default size and position loading user properties:
  127.     GUIUtilities.loadGeometry(frame, "macros-show-threads");
  128.     frame.setVisible(true);
  129.  
  130.     return frame;
  131. }
  132.  
  133.  
  134. createThreadsFrame();
  135.  
  136. /*
  137.  
  138. Macro index data (in DocBook format)
  139.  
  140.   <listitem>
  141.     <para><filename>Show_Threads.bsh</filename></para>
  142.     <abstract><para>
  143.       Displays all running Java threads in a tree.
  144.     </para></abstract>
  145.   </listitem>
  146.  
  147. */
  148.  
  149. // end Show_Threads.bsh
  150.  
  151.